package com.ruitu.mall_98_2.mine;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.beanu.arad.Arad;
import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.view.IdentifyingCode;
import com.beanu.arad.widget.EditTextCleanView;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_2.R;
import com.ruitu.mall_98_2.dao.APIRetrofit;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class UpdateTelActivity extends MyBaseActivity {

    private EditTextCleanView et_forget_tel;
    private EditTextCleanView et_register_version_code;
    private EditTextCleanView et_iv_verifactiion_code;
    private TextView tv_confirm_update;
    private TextView tv_get_code;
    private ImageView iv_verifactiion_code;

    private int time = 60;
    private boolean isGeting = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_update_tel;
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }

    @Override
    protected void initViews() {
        super.initViews();
        setHeadTitle("修改手机号");
        et_forget_tel = findViewById(R.id.et_forget_tel);
        et_register_version_code = findViewById(R.id.et_register_version_code);
        tv_confirm_update = findViewById(R.id.tv_confirm_update);
        tv_get_code = findViewById(R.id.tv_get_code);
        iv_verifactiion_code = findViewById(R.id.iv_verifactiion_code);
        et_iv_verifactiion_code = findViewById(R.id.et_iv_verifactiion_code);
        tv_confirm_update.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
        iv_verifactiion_code.setOnClickListener(this);
        getVerifactionCode();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (tv_confirm_update == v) {
            if (TextUtils.isEmpty(et_forget_tel.getText())) {
                ToastUtils.showShort("手机号码不能为空");
                return;
            }
            if (TextUtils.isEmpty(et_register_version_code.getText())) {
                ToastUtils.showShort("验证码不能为空");
                return;
            }
            showProgress();
            setUpdateTel();
        }

        if (tv_get_code == v) {
            if (TextUtils.equals(et_iv_verifactiion_code.getText().toString().toLowerCase(), resultCode)) {
                tv_get_code.setEnabled(false);
                setGetCodeTime(60);
                isGeting = true;
                handler.sendEmptyMessageDelayed(979, 1000);
                getVersionCode();
            } else {
                ToastUtils.showShort("图片验证不正确");
            }
            getVerifactionCode();
        }

        if (iv_verifactiion_code == v) {
            getVerifactionCode();
        }
    }

    String resultCode;

    private void getVerifactionCode() {
        iv_verifactiion_code.setImageBitmap(IdentifyingCode.getInstance().createBitmap());
        resultCode = IdentifyingCode.getInstance().getCode().toLowerCase();
    }

    //获取验证码
    public void getVersionCode() {
        APIRetrofit.getDefault().smsSend(et_forget_tel.getText().toString(), "4").subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort("获取验证码失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        if (baseResult.isSuccess()) {
                            ToastUtils.showShort(baseResult.getMsg());
                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 979) {
                if (isGeting) {
                    setGetCodeTime(--time);
                    handler.sendEmptyMessageDelayed(979, 1000);
                }
            }
        }
    };

    private void setGetCodeTime(int time) {
        if (time == 0) {
            isGeting = false;
            tv_get_code.setEnabled(true);
            tv_get_code.setText("获取验证码");
            this.time = 60;
        } else {
            tv_get_code.setText(time + "s后重新获取");
        }
    }

    private void setUpdateTel() {
        APIRetrofit.getDefault().updatePhone(AppHolder.instance().getUser().getId(), et_forget_tel.getText().toString(), et_register_version_code.getText().toString())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new SubscriberImpl<BaseResult>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        ToastUtils.showShort("修改失败");
                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        hideProgress();
                        if (baseResult.isSuccess()) {
                            ToastUtils.showShort(baseResult.getMsg());
                            Arad.preferences.putString("tel", et_forget_tel.getText().toString()).flush();
                            finish();

                        } else {
                            ToastUtils.showShort(baseResult.getMsg());
                        }
                    }
                });
    }
}
